﻿Imports System.Data.SqlClient

Public Class adm_merchantbrief
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then
            If (Request("uid") <> Nothing And Request("uid") <> "") Then
                Dim content, strErr, memberId, photoPath, companyId, pId As String
                strErr = ""
                memberId = Request("uid")
                Dim fileFullName, fileName As String

                If (Request("submitmerchantbrief") <> Nothing And Request("submitmerchantbrief") <> "") Then
                    content = Request("c_content")
                    ps.strSql = "select mi.* from member m,merchant_info mi where m.company_id=mi.company_id and mi.lang_flag='en' and m.GAPortal_ID=@memberId"
                    ps.parameters.Clear()
                    ps.addParameter("memberId", memberId)
                    Dim conn As SqlConnection = objfind.getConn()
                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    If (strErr = "" And objrd.Read()) Then
                        companyId = objrd.Item("company_id")
                        ps.strSql = "update merchant_info set brief=@brief where id=@id"
                        ps.parameters.Clear()
                        ps.addParameter("brief", content)
                        ps.addParameter("id", objrd.Item("id"))
                        objfind.es(ps, strErr)
                        brief.InnerHtml = content
                        If image.Visible Then
                            If (image.PostedFile.FileName <> Nothing) Then
                                pId = System.Guid.NewGuid().ToString()
                                fileFullName = image.PostedFile.FileName
                                fileName = fileFullName.Substring(fileFullName.LastIndexOf(".") + 1)
                                System.IO.Directory.CreateDirectory(Server.MapPath("../../") & "pic/company/" & objrd.Item("company_id"))
                                image.PostedFile.SaveAs(Server.MapPath("../../") & "pic/company/" & objrd.Item("company_id") & "/" & pId & "." & fileName)
                                photoPath = "/pic/company/" & objrd.Item("company_id") & "/" & pId & "." & fileName
                                ps.strSql = "update merchant set photo_path = @photoPath where company_id=@companyId"
                                ps.parameters.Clear()
                                ps.addParameter("photoPath", photoPath)
                                ps.addParameter("companyId", companyId)
                                objfind.es(ps, strErr)
                                Me.company_id.Value = companyId
                                imageDiv.InnerHtml = "<img src=""" & photoPath & """ width=""100"" height=""100"" />"
                                image.Visible = False
                            End If
                        End If
                    Else

                        ps.strSql = "select * from merchant where owner_id =@ownerId"
                        ps.addParameter("ownerId", memberId)
                        objrd = objfind.dr(ps, strErr, conn, objrd)
                        If (objrd.Read()) Then
                            companyId = objrd.Item("company_id")
                        End If

                        ps.strSql = "insert into merchant_info(id,company_id,brief,lang_flag)" & _
                                    "values (@mid,@companyId,@brief,@langFlag)"

                        ps.addParameter("mid", System.Guid.NewGuid().ToString())
                        ps.addParameter("companyId", companyId)
                        ps.addParameter("brief", content)
                        ps.addParameter("langFlag", "en")

                        objfind.es(ps, strErr)
                    End If
                    objfind.closeConn(conn)
                Else
                    sSQL = "select mi.*,mm.company_mode,mm.company_major from member m,merchant mm,merchant_info mi where m.company_id=mi.company_id and mm.company_id=m.company_id and mi.lang_flag='en' and m.GAPortal_ID=@memberId"
                    ps.strSql = sSQL
                    ps.parameters.Clear()
                    ps.addParameter("memberId", memberId)
                    Dim conn As SqlConnection = objfind.getConn()
                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    If (strErr = "" And objrd.Read()) Then
                        companyId = objrd.Item("company_id")
                        brief.InnerHtml = objrd.Item("brief").ToString
                        ps.strSql = "select * from merchant where company_id=@companyId"
                        ps.parameters.Clear()
                        ps.addParameter("companyId", companyId)
                        objrd = objfind.dr(ps, strErr, conn, objrd)
                        objrd.Read()
                        If (objrd.Item("photo_path").ToString <> Nothing And objrd.Item("photo_path").ToString <> "") Then
                            imageDiv.InnerHtml = "<img src=""" & objrd.Item("photo_path") & """ width=""100"" height=""100"" />"
                            Me.company_id.Value = companyId
                            image.Visible = False
                        Else
                            image.Visible = True
                        End If
                    End If
                    objfind.closeConn(conn)
                End If


            Else
                Response.Redirect("../memberMngt_default.aspx")
            End If
        Else
            Response.Redirect("../adm_login.aspx")
        End If
    End Sub

    Protected Sub deleteImage_Click(ByVal sender As Object, ByVal e As EventArgs) Handles deleteImage.Click
        Dim companyId, strErr, photoPath As String
        strErr = ""
        companyId = Me.company_id.Value
        If (companyId <> Nothing And companyId.Trim() <> "") Then
            ps.strSql = "select * from merchant where company_id=@companyId"
            ps.parameters.Clear()
            ps.addParameter("companyId", companyId)
            Dim conn As SqlConnection = objfind.getConn()
            objrd = objfind.dr(ps, strErr, conn, objrd)
            objrd.Read()
            photoPath = objrd.Item("photo_path").ToString
            If (photoPath <> Nothing And photoPath <> "") Then
                ps.strSql = "update merchant set photo_path = NULL where company_id=@companyId"
                ps.parameters.Clear()
                ps.addParameter("companyId", companyId)
                ' ps.addParameter("photoPath", vbNull)
                objfind.es(ps, strErr)
                imageDiv.InnerHtml = ""
                image.Value = ""
                image.Visible = True
                System.IO.File.Delete(Server.MapPath("../../") & photoPath)
            End If
            objfind.closeConn(conn)
        End If
    End Sub

End Class